package org.example.blogserver.system.service.impl;


import com.github.pagehelper.PageHelper;
import org.example.blogpojo.blog.domain.Role;
import org.example.blogpojo.blog.query.RoleQuery;
import org.example.blogserver.system.mapper.RoleMapper;
import org.example.blogserver.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
// 查询事务控制方式：设置只读 支持当前事务，如果没有当前事务，就以非事务方法执行。
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void addRole(Role role) {
        roleMapper.addRole(role);
        if (role.getPermissionids().size()>0){
            roleMapper.addRoleAndPs(role);
        }
        if (role.getMenuids().size()>0){
            roleMapper.addRoleAndMs(role);
        }
    }

    @Override
    @Transactional
    public void updateRole(Role role) {
        roleMapper.updateRole(role);

        roleMapper.delRoleAndPs(role.getId());
        roleMapper.delRoleAndMs(role.getId());

        if (role.getPermissionids().size()>0){
            roleMapper.addRoleAndPs(role);
        }

        if (role.getMenuids().size()>0){
            roleMapper.addRoleAndMs(role);
        }
    }

    @Override
    @Transactional
    public void deteleById(Long id) {
        roleMapper.deteleById(id);

        roleMapper.delRoleAndPs(id);
        roleMapper.delRoleAndMs(id);
    }

    @Override
    @Transactional
    public void deleteRoleByIds(Long[] ids) {
        roleMapper.deleteRoleByIds(ids);
        for (Long id : ids) {
            roleMapper.delRoleAndPs(id);
            roleMapper.delRoleAndMs(id);
        }
    }

    @Override
    public Role findOne(Long id) {
        return roleMapper.findOne(id);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    @Override
    public List<Role> queryPage(RoleQuery roleQuery) {
        //开始设置分页信息
        //干了三件事情：
        //1.查询总条数 2.根据驱动名称自动添加分页 3.分页查询返回结果
        PageHelper.startPage(roleQuery.getPageNum(), roleQuery.getPageSize());
        //直接高级查询，自动实现分页
        //这个拿的是分页后的所有数据，只需要在这个里面缺少补啥就行了
        List<Role> roles = roleMapper.queryPage(roleQuery);//ctr 1

        //拿到这些数据的id的集合，方便后续通过中间表拿到需要数据的id
        List<Long> rolesIds = roles.stream().map(Role::getId).collect(Collectors.toList());
        //拿到需要的数据的id集合并封装到实体类中，没有就在实体类中添加字段
        List<Role> rolesPermissionIds= roleMapper.rolesPermissionIds(rolesIds);
        //因为是一对多的关系所以使用map集合存 ，方便取数据到上面所有数据的集合中，角色id-》角色拥有的权限 1对多的关系
        //{1:[1,2,3,4],2:[1,2,5,7]}这样存储好拿数据
        Map<Long, List<Long>> rolesPermission = rolesPermissionIds.stream().collect(Collectors.toMap(Role::getId, Role::getPermissionids));
        for (Role role : roles) {
            List<Long> longs = rolesPermission.get(role.getId());
            role.setPermissionids(longs);
        }

        List<Role> rolesMenuIds= roleMapper.rolesMenuIds(rolesIds);
        Map<Long, List<Long>> rolesMenuMap = rolesMenuIds.stream().collect(Collectors.toMap(Role::getId, Role::getMenuids));
        for (Role role : roles) {
            List<Long> longs = rolesMenuMap.get(role.getId());
            role.setMenuids(longs);
        }

        return roles;
    }
}
